import request from '@/utils/request'
import { otherFiltersItem } from '@/utils'

/**
 * 保存日常走访主表数据
 * @param {Object} data
 * @returns
 */
export function saveDailyPatrol(data) {
  return request({
    url: `/api/v1/dailyPatrol`,
    method: data.id ? 'put' : 'post',
    data
  })
}
/**
 * 单个删除主表数据
 * @param {String} id
 * @returns
 */
export function deleteTableItem(id) {
  return request({
    url: `/api/v1/dailyPatrol/${id}`,
    method: 'delete'
  })
}

/**
 * 根据shopCode获取门店详情
 * @param {string} shopCode
 * @returns
 */
export function getShopDetailByCode(shopCode) {
  return request({
    url: `/api/v1/dailyPatrol/getShopByCode/${shopCode}`,
    method: 'get'
  })
}

/**
 * 根据走访id获取主表详情
 * @param {string} id 走访id
 * @returns
 */
export function queryFormDetailById(id) {
  return request({
    url: `/api/v1/dailyPatrol/${id}`,
    method: 'get'
  })
}
/**
 * 保存门店变更单条数据
 * @param {Object} data
 * @returns
 */
export const saveShopChange = async(data) => {
  const res = await request({
    url: `/api/v1/patrolShopChangeInformation`,
    method: data.id ? 'put' : 'post',
    data
  })
  return res && res.msg
}
/**
 * 根据上层走访id查询门店变更列表
 * @param {string} patrolId 走访id
 * @returns
 */
export function queryShopChangeList(patrolId, shopCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('patrolId', patrolId, '='), otherFiltersItem('shopCode', shopCode, '=')]))
  return request({
    url: `/api/v1/patrolShopChangeInformation/page`,
    method: 'post',
    data
  })
}

/**
 * 根据上层走访id查询走访图片列表
 * @param {string} patrolId 走访id
 * @returns
 */
export function queryPatrolPicList(patrolId) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('patrolId', patrolId, '=')]))
  return request({
    url: `/api/v1/patrolKeyArea/page`,
    method: 'post',
    data
  })
}

/**
 * 获取附件配置
 * @returns
 */
export function postConfigAttachmentTemplate() {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('belongModel', 'SHOP_INTERVIEW', '=')]))
  return request({
    url: '/api/v1/configAttachmentTemplate/page',
    method: 'POST',
    data
  })
}
/**
 * 变更走访人员
 * @param {Object} data
 * @returns
 */
export function updatePerson(data) {
  return request({
    url: '/api/v1/dailyPatrol/updatePerson',
    method: 'put',
    data
  })
}
/**
 * 获取责任列表
 * @param {*} type
 * @returns
 */
export function queryDutyList(type) {
  return request({
    url: `/api/v1/patrolKeyArea/getProblemDutyList`,
    method: 'get',
    params: { type }
  })
}

/**
 * 获取责任列表-树形结构
 * @param {*} type
 * @returns
 */
export function queryDutyListTree(type) {
  return request({
    url: `/api/v1/patrolKeyArea/getDutyList`,
    method: 'get',
    params: { type }
  })
}

/**
 * 保存重点区域图片数据
 * @param {Object} data
 * @returns
 */
export function savePatrolKeyArea(data) {
  return request({
    url: `/api/v1/patrolKeyArea`,
    method: data.id ? 'put' : 'post',
    data
  })
}

/**
 * 保存其他区域附件
 * @param {Array} data
 * @returns
 */
export function saveDailyPatrolFile(data) {
  return request({
    url: `/api/v1/dailyPatrol/addFile`,
    method: 'post',
    data
  })
}
/**
 * 根据上层走访id查询问题列表
 * @param {string} patrolId 巡检id
 * @returns
 */
export function queryPatrolQuestionList(patrolId) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('patrolId', patrolId, '=')]))
  return request({
    url: `/api/v1/patrolQuestion/page`,
    method: 'post',
    data
  })
}

/**
 * 保存走访问题
 * @param {Object} data
 * @returns
 */
export function savePatrolQuestionList(data) {
  return request({
    url: `/api/v1/patrolQuestion`,
    method: data.id ? 'put' : 'post',
    data
  })
}

/**
 * 删除单个走访问题数据
 * @param {string} id
 * @returns
 */
export function deletePatrolQuestion(id) {
  return request({
    url: `/api/v1/patrolQuestion/${id}`,
    method: 'delete'
  })
}
/**
 * 删除单个走访人员
 * @param {string} id
 * @returns
 */
export function deletePersonCriticize(id) {
  return request({
    url: `/api/v1/patrolPersonCriticize/${id}`,
    method: 'delete'
  })
}
/**
 * 保存走访人员
 * @param {Object} data
 * @returns
 */
export function savePersonCriticize(data) {
  return request({
    url: `/api/v1/patrolPersonCriticize`,
    method: data.id ? 'put' : 'post',
    data
  })
}

/**
 * 根据门店编码、类型获取用户信息,仅支持 办事处经理、成品客户经理、定制客户经理
 * @param {String} shopCode
 * @param {String} type 人员类型
 * @returns
 */
export function queryPersonInfo(shopCode, type) {
  return request({
    url: `/api/v1/patrolPersonCriticize/getUserInfoByType/${shopCode}/${type}`,
    method: 'get'
  })
}
/**
 * 根据走访id获取评分类型
 * @param {*} patrolId
 * @returns
 */
export function queryEvaluateTypeById(patrolId) {
  return request({
    url: `/api/v1/patrolShopScore/getScoreType/${patrolId}`,
    method: 'get'
  })
}

/**
 * 门店评分列表
 * @param {*} patrolId
 * @param {*} decorateTypeId 评分类型id
 * @returns
 */
export function queryShopScoreList(patrolId, decorateTypeId) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('patrolId', patrolId, '='), otherFiltersItem('decorateTypeId', decorateTypeId, '=')]))
  return request({
    url: `/api/v1/patrolShopScore/page`,
    method: 'post',
    data
  })
}
/**
 * 评分类型列表
 * @param {*} patrolId
 * @returns
 */
export function queryEvaluateTypeList(patrolId) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('enable', 1, '=')]))
  return request({
    url: `/api/v1/decorateType/page`,
    method: 'post',
    data
  })
}

/**
 * 根据评价类型id获取了评价类别详情
 * @param {String} decorateTypeId 评价类型id
 * @returns
 */
export function queryEvaluateDetailList(decorateTypeId) {
  return request({
    url: `/api/v1/decorateItem/getListByTypeId/${decorateTypeId}`,
    method: 'get'
  })
}
/**
 * 走访异常反馈保存
 * @param {Object} data
 * @returns
 */
export function saveExceptionFeedback(data) {
  return request({
    url: `/api/v1/patrolAbnormalFeedback`,
    method: data.id ? 'put' : 'post',
    data
  })
}

/**
 * 删除单个走访异常反馈
 * @param {string} id
 * @returns
 */
export function deleteExceptionFeedback(id) {
  return request({
    url: `api/v1/patrolAbnormalFeedback/${id}`,
    method: 'delete'
  })
}

/**
 * 项目专员异常反馈
 * @param {*} patrolId
 * @param {*} describe
 * @returns
 */
export function saveAbnormalFeedback(patrolId, describe) {
  return request({
    url: `api/v1/patrolAbnormalFeedback/changeDescribe/${patrolId}/${describe}`,
    method: 'put'
  })
}
/**
 * 删除单个走访专项
 * @param {string} id
 * @returns
 */
export function deleteSpecialItem(id) {
  return request({
    url: `api/v1/patrolSpecial/${id}`,
    method: 'delete'
  })
}

/**
 * 保存走访专项
 * @param {*} data
 * @returns
 */
export function saveSpecialItem(data) {
  return request({
    url: `/api/v1/patrolSpecial`,
    method: data.id ? 'put' : 'post',
    data
  })
}

/**
 * 审核
 * @param {object} data
 * @returns
 */
export function saveAudit(data) {
  return request({
    url: `/api/v1/dailyPatrol/examine`,
    method: 'put',
    data
  })
}

/**
 * 根据主数据id查询节点记录
 * @param {String} sourceId patrolId
 * @returns
 */
export function queryNodeList(sourceId) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('sourceId', sourceId, '=')]))
  return request({
    url: `/api/v1/patrolProcessLog/page`,
    method: 'post',
    data
  })
}
/**
 * 走访专项提交
 * @param {*} remark
 * @param {*} id 日常走访id
 * @returns
 */
export function submitAudit({ id, remark }) {
  return request({
    url: `/api/v1/dailyPatrol/submit`,
    method: 'post',
    data: { id, remarks: remark }
  })
}

/**
 * 保存门店评分
 * @param {Object} data
 * @returns
 */
export function saveShopEvaluate(data, method = 'post') {
  return request({
    url: `/api/v1/patrolShopScore`,
    method,
    data
  })
}

/**
 * 根据验收类型查询问题分类列表
 * @returns
 */
export function queryProblemClassList() {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('acceptType', 'B', '='), otherFiltersItem('enable', '1', '=')]))
  return request({
    url: '/api/v1/configAcceptProblem/page',
    method: 'POST',
    data
  })
}
